package com.alibaba.dongbao.utils;

import org.apache.commons.lang3.ObjectUtils;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: Mr.ZJW
 * @Date: 2022-05-05 10:59
 * @Description: Map排序工具类
 */
public class MapSortUtil {

    /**
     * @Author: Mr.ZJW
     * @Description: Map排序工具类
     * @Date: 2022/5/5 11:01
     **/
    public static Map<String, Object> sortMapByKey(Map<String, Object> map) {
        //判断是否为空
        if (ObjectUtils.isEmpty(map)) {
            throw new RuntimeException("输入参数为空");
        }
        //排序
        Map<String, Object> sortMap = new TreeMap<>(new MyMapComparator());
        sortMap.putAll(map);
        return sortMap;
    }

    static class MyMapComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    }

}
